﻿using System;
using System.Data;
using System.Text;
using System.Data.OleDb;
using DTcms.DBUtility;//Please add references
using DTcms.DBUtility;
using DTcms.Common;
namespace DTcms.DAL
{
	/// <summary>
	/// 数据访问类:dt_erp
	/// </summary>
	public partial class dt_erp
	{
		public dt_erp()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperOleDb.GetMaxID("id", "dt_erp"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from dt_erp");
			strSql.Append(" where id=@id");
			OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)
			};
			parameters[0].Value = id;

			return DbHelperOleDb.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(DTcms.Model.dt_erp model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into dt_erp(");
			strSql.Append("user_id,user_addtime,user_lasttime,epr_name,erp_Add,erp_web,erp_email,erp_number,erp_contacts,erp_phonenumber,erp_sources,erp_bak1,erp_bak2,erp_bak3,erp_bak4,erp_bak5)");
			strSql.Append(" values (");
			strSql.Append("@user_id,@user_addtime,@user_lasttime,@epr_name,@erp_Add,@erp_web,@erp_email,@erp_number,@erp_contacts,@erp_phonenumber,@erp_sources,@erp_bak1,@erp_bak2,@erp_bak3,@erp_bak4,@erp_bak5)");
			OleDbParameter[] parameters = {
					new OleDbParameter("@user_id", OleDbType.Integer,4),
					new OleDbParameter("@user_addtime", OleDbType.Date),
					new OleDbParameter("@user_lasttime", OleDbType.Date),
					new OleDbParameter("@epr_name", OleDbType.VarChar,255),
					new OleDbParameter("@erp_Add", OleDbType.VarChar,255),
					new OleDbParameter("@erp_web", OleDbType.VarChar,255),
					new OleDbParameter("@erp_email", OleDbType.VarChar,255),
					new OleDbParameter("@erp_number", OleDbType.VarChar,255),
					new OleDbParameter("@erp_contacts", OleDbType.VarChar,255),
					new OleDbParameter("@erp_phonenumber", OleDbType.VarChar,255),
					new OleDbParameter("@erp_sources", OleDbType.Integer,4),
					new OleDbParameter("@erp_bak1", OleDbType.Integer,4),
					new OleDbParameter("@erp_bak2", OleDbType.Integer,4),
					new OleDbParameter("@erp_bak3", OleDbType.VarChar,255),
					new OleDbParameter("@erp_bak4", OleDbType.VarChar,255),
					new OleDbParameter("@erp_bak5", OleDbType.Date)};
			parameters[0].Value = model.user_id;
			parameters[1].Value = model.user_addtime;
			parameters[2].Value = model.user_lasttime;
			parameters[3].Value = model.epr_name;
			parameters[4].Value = model.erp_Add;
			parameters[5].Value = model.erp_web;
			parameters[6].Value = model.erp_email;
			parameters[7].Value = model.erp_number;
			parameters[8].Value = model.erp_contacts;
			parameters[9].Value = model.erp_phonenumber;
			parameters[10].Value = model.erp_sources;
			parameters[11].Value = model.erp_bak1;
			parameters[12].Value = model.erp_bak2;
			parameters[13].Value = model.erp_bak3;
			parameters[14].Value = model.erp_bak4;
			parameters[15].Value = model.erp_bak5;

			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(DTcms.Model.dt_erp model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update dt_erp set ");
			strSql.Append("user_id=@user_id,");
			strSql.Append("user_addtime=@user_addtime,");
			strSql.Append("user_lasttime=@user_lasttime,");
			strSql.Append("epr_name=@epr_name,");
			strSql.Append("erp_Add=@erp_Add,");
			strSql.Append("erp_web=@erp_web,");
			strSql.Append("erp_email=@erp_email,");
			strSql.Append("erp_number=@erp_number,");
			strSql.Append("erp_contacts=@erp_contacts,");
			strSql.Append("erp_phonenumber=@erp_phonenumber,");
			strSql.Append("erp_sources=@erp_sources,");
			strSql.Append("erp_bak1=@erp_bak1,");
			strSql.Append("erp_bak2=@erp_bak2,");
			strSql.Append("erp_bak3=@erp_bak3,");
			strSql.Append("erp_bak4=@erp_bak4,");
			strSql.Append("erp_bak5=@erp_bak5");
			strSql.Append(" where id=@id");
			OleDbParameter[] parameters = {
					new OleDbParameter("@user_id", OleDbType.Integer,4),
					new OleDbParameter("@user_addtime", OleDbType.Date),
					new OleDbParameter("@user_lasttime", OleDbType.Date),
					new OleDbParameter("@epr_name", OleDbType.VarChar,255),
					new OleDbParameter("@erp_Add", OleDbType.VarChar,255),
					new OleDbParameter("@erp_web", OleDbType.VarChar,255),
					new OleDbParameter("@erp_email", OleDbType.VarChar,255),
					new OleDbParameter("@erp_number", OleDbType.VarChar,255),
					new OleDbParameter("@erp_contacts", OleDbType.VarChar,255),
					new OleDbParameter("@erp_phonenumber", OleDbType.VarChar,255),
					new OleDbParameter("@erp_sources", OleDbType.Integer,4),
					new OleDbParameter("@erp_bak1", OleDbType.Integer,4),
					new OleDbParameter("@erp_bak2", OleDbType.Integer,4),
					new OleDbParameter("@erp_bak3", OleDbType.VarChar,255),
					new OleDbParameter("@erp_bak4", OleDbType.VarChar,255),
					new OleDbParameter("@erp_bak5", OleDbType.Date),
					new OleDbParameter("@id", OleDbType.Integer,4)};
			parameters[0].Value = model.user_id;
			parameters[1].Value = model.user_addtime;
			parameters[2].Value = model.user_lasttime;
			parameters[3].Value = model.epr_name;
			parameters[4].Value = model.erp_Add;
			parameters[5].Value = model.erp_web;
			parameters[6].Value = model.erp_email;
			parameters[7].Value = model.erp_number;
			parameters[8].Value = model.erp_contacts;
			parameters[9].Value = model.erp_phonenumber;
			parameters[10].Value = model.erp_sources;
			parameters[11].Value = model.erp_bak1;
			parameters[12].Value = model.erp_bak2;
			parameters[13].Value = model.erp_bak3;
			parameters[14].Value = model.erp_bak4;
			parameters[15].Value = model.erp_bak5;
			parameters[16].Value = model.id;

			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from dt_erp ");
			strSql.Append(" where id=@id");
			OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from dt_erp ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DTcms.Model.dt_erp GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,user_id,user_addtime,user_lasttime,epr_name,erp_Add,erp_web,erp_email,erp_number,erp_contacts,erp_phonenumber,erp_sources,erp_bak1,erp_bak2,erp_bak3,erp_bak4,erp_bak5 from dt_erp ");
			strSql.Append(" where id=@id");
			OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)
			};
			parameters[0].Value = id;

			DTcms.Model.dt_erp model=new DTcms.Model.dt_erp();
			DataSet ds=DbHelperOleDb.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DTcms.Model.dt_erp DataRowToModel(DataRow row)
		{
			DTcms.Model.dt_erp model=new DTcms.Model.dt_erp();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=int.Parse(row["id"].ToString());
				}
				if(row["user_id"]!=null && row["user_id"].ToString()!="")
				{
					model.user_id=int.Parse(row["user_id"].ToString());
				}
				if(row["user_addtime"]!=null && row["user_addtime"].ToString()!="")
				{
					model.user_addtime=DateTime.Parse(row["user_addtime"].ToString());
				}
				if(row["user_lasttime"]!=null && row["user_lasttime"].ToString()!="")
				{
					model.user_lasttime=DateTime.Parse(row["user_lasttime"].ToString());
				}
				if(row["epr_name"]!=null)
				{
					model.epr_name=row["epr_name"].ToString();
				}
				if(row["erp_Add"]!=null)
				{
					model.erp_Add=row["erp_Add"].ToString();
				}
				if(row["erp_web"]!=null)
				{
					model.erp_web=row["erp_web"].ToString();
				}
				if(row["erp_email"]!=null)
				{
					model.erp_email=row["erp_email"].ToString();
				}
				if(row["erp_number"]!=null)
				{
					model.erp_number=row["erp_number"].ToString();
				}
				if(row["erp_contacts"]!=null)
				{
					model.erp_contacts=row["erp_contacts"].ToString();
				}
				if(row["erp_phonenumber"]!=null)
				{
					model.erp_phonenumber=row["erp_phonenumber"].ToString();
				}
				if(row["erp_sources"]!=null && row["erp_sources"].ToString()!="")
				{
					model.erp_sources=int.Parse(row["erp_sources"].ToString());
				}
				if(row["erp_bak1"]!=null && row["erp_bak1"].ToString()!="")
				{
					model.erp_bak1=int.Parse(row["erp_bak1"].ToString());
				}
				if(row["erp_bak2"]!=null && row["erp_bak2"].ToString()!="")
				{
					model.erp_bak2=int.Parse(row["erp_bak2"].ToString());
				}
				if(row["erp_bak3"]!=null)
				{
					model.erp_bak3=row["erp_bak3"].ToString();
				}
				if(row["erp_bak4"]!=null)
				{
					model.erp_bak4=row["erp_bak4"].ToString();
				}
				if(row["erp_bak5"]!=null && row["erp_bak5"].ToString()!="")
				{
					model.erp_bak5=DateTime.Parse(row["erp_bak5"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,user_id,user_addtime,user_lasttime,epr_name,erp_Add,erp_web,erp_email,erp_number,erp_contacts,erp_phonenumber,erp_sources,erp_bak1,erp_bak2,erp_bak3,erp_bak4,erp_bak5 ");
			strSql.Append(" FROM dt_erp ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperOleDb.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM dt_erp ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            object obj = DbHelperOleDb.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from dt_erp T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperOleDb.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			OleDbParameter[] parameters = {
					new OleDbParameter("@tblName", OleDbType.VarChar, 255),
					new OleDbParameter("@fldName", OleDbType.VarChar, 255),
					new OleDbParameter("@PageSize", OleDbType.Integer),
					new OleDbParameter("@PageIndex", OleDbType.Integer),
					new OleDbParameter("@IsReCount", OleDbType.Boolean),
					new OleDbParameter("@OrderType", OleDbType.Boolean),
					new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
					};
			parameters[0].Value = "dt_erp";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperOleDb.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM dt_erp");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperOleDb.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperOleDb.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
    }
}

